package com.hebeu.gfh.controller;

import com.alibaba.fastjson.JSONObject;
import com.hebeu.gfh.pojo.po.User;
import com.hebeu.gfh.pojo.vo.appinfo;
import com.hebeu.gfh.pojo.vo.loginInfo;
import com.hebeu.gfh.pojo.vo.updatepwd;
import com.hebeu.gfh.service.userService;
import com.hebeu.gfh.utils.JwtUtil;
import com.hebeu.gfh.utils.R;
import com.hebeu.gfh.utils.WechatTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.ClassInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: 刘剑利
 * @Version: v1.0
 */
@RestController
@RequestMapping("/user")
public class userController {

    @Autowired
    userService userService;

//    @Autowired
//    WechatTools wechatTools;
    /**
     * 微信小程序的登陆，如果有账号，则返回登陆成功，如果没有则返回（首次用户，需要认证)
     * @return
     */
//    @PostMapping("/login")
//    public RsJsonBean LoginUser(@RequestBody JSONObject obj){
//
//        userService.LoginUser(obj.getString("code"));
//
//    return null;
//}
    @PostMapping("/getWxInfoTest")
    public String getWxInfoTest(@RequestBody JSONObject obj) {
        String AppId = "wx6d975b98d1e693f6";
        String AppSecret = "de36597d5f296a8137678342dff771de";
        WechatTools wechatTools = new WechatTools();
        JSONObject wxJson = JSONObject.parseObject(WechatTools.getOpenid(obj.getString("code"), AppId, AppSecret));
        return wxJson.toJSONString();
    }
    //登录
    @PostMapping("/login")
    public R login(loginInfo loginInfo){
        appinfo result = userService.login(loginInfo);
        if(result != null){
            JwtUtil.createToken(result);
            return R.ok(result);
        }
       return R.error(500,"登录失败");
    }


    //管理员登录
    @PostMapping("/adminLogin")
    public R adminLogin(@RequestBody loginInfo loginInfo){
        appinfo result = userService.adminLogin(loginInfo);
        if(result != null){
            JwtUtil.createToken(result);
            return R.ok(result);
        }
        return R.error(500,"登录失败");
    }

    //添加学生(这里是管理员进行注册，默认账号，密码是学号，然后学生可以登上去修改信息)
    @PostMapping("/addUser")
    public R addUser(User user){
        try {

            userService.addUser(user);
        }catch (Exception e){
            return R.error(500,"添加学生失败");
        }
        return R.ok();
    }
    //添加管理员
    @PostMapping("/addAamin")
    public R addAdmin(User user){
        try {
            userService.addAdmin(user);
        }catch (Exception e){
            return R.error(500,"添加管理员失败");
        }
        return R.ok();
    }
    //删除用户
    @PostMapping("/deleteUser")
    public R deleteUser(int userId){
        try {
            userService.deleteUser(userId);
        }catch (Exception e){
            return R.error(500,"删除用户失败");
        }
        return R.ok();
    }
    //根据用户id查询用户信息
    @PostMapping("/findUserByuserId")
    public R findUserByuserId(int userId){
        appinfo user = userService.findUserByuserId(userId);
        return R.ok("查询成功",user);
    }
    //修改用户信息
    @PostMapping("/updateUserByuserId")
    public R updateUserByuserId(User user){
        appinfo user1=null;
        try {
            userService.updateUserByuserId(user);
            user1 = userService.findUserByuserId(user.getUserId());
        }catch (Exception e){
            System.out.println(e);
            return R.error(500,"更新用户失败");
        }      return R.ok(user1);
    }
    //根据班级信息查询所属学生
    @PostMapping("/findUserByClassInfo")
    public R findUserByClassInfo(ClassInfo classInfo){
        List<User> userList = userService.findUserByClassInfo(classInfo);
        return R.ok("查询成功",userList);
    }

    //修改密码
    @PostMapping("/updatepwd")
    public  R  updatepwd(updatepwd updatepwd){
        System.out.println(updatepwd);
        try {
            appinfo appinfo =userService.findUserByuserId(updatepwd.getUserId());
            String password =appinfo.getUserInfo().getPassword();
            if(!password.equals(updatepwd.getOldpassword())){
                return R.error(501,"密码不正确");
            }
            userService.updatepwd(updatepwd);
        }catch (Exception e){
            System.out.println("异常为"+e);
            System.out.println("-------------------------");
            return R.error(500,"更新失败");
        }
        return R.ok("更新成功");
    }
}
